﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Windows.Forms;

namespace TRASOutlook
{
    partial class FR_NewsTask
    {
        #region Form Region Factory

        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass("IPM.Task.PublishNews")]
        [Microsoft.Office.Tools.Outlook.FormRegionName("TRASOutlook.FR_NewsTask")]
        public partial class FR_NewsTaskFactory
        {
            // Occurs before the form region is initialized.
            // To prevent the form region from appearing, set e.Cancel to true.
            // Use e.OutlookItem to get a reference to the current Outlook item.
            private void FR_NewsTaskFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {
            }
        }

        #endregion

        public Outlook.TaskItem task;

        // Occurs before the form region is displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void FR_NewsTask_FormRegionShowing(object sender, System.EventArgs e)
        {
            task = this.OutlookItem as Outlook.TaskItem;
            tx_TaskDescription.Text = task.Subject;
            tx_StartTime.Text = task.StartDate.ToShortDateString();
            tx_EndTime.Text = task.DueDate.ToShortDateString();
            tx_Director.Text = task.UserProperties["coordinator"].Value.ToString();
            tx_Operator.Text = task.UserProperties["operator"].Value.ToString();
            if (task.Importance != Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh)
            {
                rd_Normal.Checked = true;
            }
            else
            {
                rd_Important.Checked = true;
            }

            tx_NewsTitle.Text = task.UserProperties["title"].Value.ToString();
            tx_NewLink.Text = task.UserProperties["linkaddr"].Value.ToString();
            tx_Memo.Text = task.UserProperties["memo"].Value.ToString();
        }

        // Occurs when the form region is closed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void FR_NewsTask_FormRegionClosed(object sender, System.EventArgs e)
        {
            Save(); 
        }

        private void btn_Submit_Click(object sender, EventArgs e)
        {
            Save();
            CTaskMgr.Instance().CompleteNewsTask(task);
            task.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olSave);
            
        }

        private void Save()
        {
            task.UserProperties["title"].Value = tx_NewsTitle.Text;
            task.UserProperties["linkaddr"].Value = tx_NewLink.Text;
            task.UserProperties["memo"].Value = tx_Memo.Text;
            task.Save();
        }
    }
}
